//
//  HeaderFile.swift
//  SwiftStudy
//
//  Created by chang on 2018/1/11.
//  Copyright © 2018年 chang. All rights reserved.
//

import UIKit

/**
 *函数修饰,解释
 *
 * public：可以访问自己模块中任何 public，被 import 到其他模块，也可以被访问
 * internal：只能访问自己模块中任何 internal 实体，不能访问其他模块中的 internal 实体
 * private：私有，只能在当前源文件中使用。
 *
 */

//system version
let SystemVersionString : NSString = UIDevice.current.systemVersion as NSString

let StatusBarHeight = UIApplication.shared.statusBarFrame.size.height
//screen width,height
let  MAINSCREEN_WIDTH = UIScreen.main.bounds.width
let  MAINSCREEN_HEIGHT = UIScreen.main.bounds.height

//color
func UIColorFromRGB(rgbValue: UInt) -> UIColor {
    return UIColor(
        red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
        green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
        blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
        alpha: CGFloat(1.0)
    )
}

//add line color e0e0e0
func addViewLine(sView: UIView) -> () {
    let lineView = UIView.init(frame: CGRect.init(x: 0, y: sView.bounds.size.height-0.5, width: sView.bounds.size.width, height: 0.5))
    lineView.backgroundColor = UIColor.defaultLineColor
    sView.addSubview(lineView)
}

//font size
func SizeWithFont(content : NSString, attrDic : NSDictionary, maxSize : CGSize) -> CGSize {
    return content.boundingRect(with: maxSize, options: (NSStringDrawingOptions(rawValue: NSStringDrawingOptions.usesLineFragmentOrigin.rawValue | NSStringDrawingOptions.usesFontLeading.rawValue)), attributes: attrDic as? [NSAttributedStringKey : Any], context: nil).size
}

//UserDefaults--data save
func SaveUserDefaults(key: NSString, value: AnyObject) {
    UserDefaults.standard.set(value, forKey: key as String)
    UserDefaults.standard.synchronize()
}
//get
func GetUserDefaults(key: NSString) -> AnyObject {
    let value = UserDefaults.standard.object(forKey: key as String)
    return value as AnyObject
}
//remove
func RemoveUserDefaults(key: NSString) {
    UserDefaults.standard.removeObject(forKey: key as String)
}
let SearchText_Keyword : NSString = "SearchText_Keyword"


//screen proportion
func CCScaleW(w: CGFloat) -> CGFloat {
    return (w * 2) / 750 * MAINSCREEN_WIDTH
}
func CCScaleH (h: CGFloat) -> CGFloat {
    return (h * 2) / 1334 * MAINSCREEN_HEIGHT
}

/**对UIView扩展属性，类似oc分类*/
extension UIView {
    // .x
    public var left: CGFloat {
        get {
            return self.frame.origin.x
        }
        set {
            var rect = self.frame
            rect.origin.x = newValue
            self.frame = rect
        }
    }
    
    // .y
    public var top: CGFloat {
        get {
            return self.frame.origin.y
        }
        set {
            var rect = self.frame
            rect.origin.y = newValue
            self.frame = rect
        }
    }
    
    // .maxX
    public var right: CGFloat {
        get {
            return self.frame.maxX
        }
    }
    
    // .maxY
    public var bottom: CGFloat {
        get {
            return self.frame.maxY
        }
    }
    
    // .centerX
    public var centerX: CGFloat {
        get {
            return self.center.x
        }
        set {
            self.center = CGPoint(x: newValue, y: self.center.y)
        }
    }
    
    // .centerY
    public var centerY: CGFloat {
        get {
            return self.center.y
        }
        set {
            self.center = CGPoint(x: self.center.x, y: newValue)
        }
    }
    
    // .width
    public var width: CGFloat {
        get {
            return self.frame.size.width
        }
        set {
            var rect = self.frame
            rect.size.width = newValue
            self.frame = rect
        }
    }
    
    // .height
    public var height: CGFloat {
        get {
            return self.frame.size.height
        }
        set {
            var rect = self.frame
            rect.size.height = newValue
            self.frame = rect
        }
    }
    // removeAllSubviews
    func removeAllSubviews() {
        while self.subviews.count<=0 {
            self.subviews.last?.removeFromSuperview()
        }
    }
}

extension UIColor {
    //text color
    class var defaultTextColor : UIColor {
        return UIColorFromRGB(rgbValue: 0x333333)
    }
    //background color
    class var defaultBackgroundColor : UIColor {
        return UIColorFromRGB(rgbValue: 0xf0f1f2)
    }
    // yellow color
    class var defaultYellowColor : UIColor {
        return UIColorFromRGB(rgbValue: 0xfaa202)
    }
    // red color
    class var defaultRedColor : UIColor {
        return UIColorFromRGB(rgbValue: 0xE63D52)
    }
    // line color
    class var defaultLineColor : UIColor {
        return UIColorFromRGB(rgbValue: 0xE0E0E0)
    }
}

extension UIViewController {
    
    //The navigation bar height
    var navgationBarHeight : CGFloat {
        return self.navigationController!.navigationBar.frame.size.height
    }
    
    //The navigation bar bottom
    var navgationBarBottom : CGFloat {
        return self.navigationController!.navigationBar.bottom
    }
    
    //The tab bar height
    var tabBarHeight : CGFloat {
        return self.tabBarController!.tabBar.frame.size.height
    }
    
}



